Column

Mean Min-temperature (celsius) by year

Column

Daily temperature difference (celsius) Boxplot by season

Mean Min-temperature(celsius) by Month and color by mean Precipitation (mm)

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{r setup2, include=FALSE}

library(flexdashboard)
# library(dplyr)
# library(magrittr)

# library(plotly)

# library(patchwork)

# library(ggplot2)

# Set a seed
# set.seed(777)
library(p8105.datasets)
library(tidyverse)
library(dplyr)
library(plotly)

```



```{r Prepare data, echo = FALSE, warning = FALSE, message = FALSE}
# library(ggplot2)

data(ny_noaa)
# data(nyc_airbnb)

year_filter = '2017'


ny_noaa = 
  ny_noaa %>% 
  janitor::clean_names() %>% 
  # Pick 5 stations with few missing values
  filter(id %in% c('USC00300055', 'USC00301401', 'USC00303025', 'USC00300889', 'USC00303346')) %>%  
  drop_na() %>% 
  mutate(tmax = as.double(tmax)/10,
         tmin = as.double(tmin)/10) %>% 
  rename(station_id = id) 

```

Column {data-width=650}
-----------------------------------------------------------------------

### Mean Min-temperature (celsius) by year

```{r Mean_tmin by Year, echo = FALSE, warning = FALSE, message = FALSE}
# library(plotly)
mean_tmin_df = 
  ny_noaa %>% 
  mutate(year = lubridate::year(date)) %>% 
  group_by(year, station_id) %>% 
  summarise(mean_tmin = mean(tmin))

Boxplot = 
  ggplot(mean_tmin_df, aes(x = year, y = mean_tmin, color = station_id)) + 
  geom_line() +
  ylab('Mean Min-temperature (celsius)') +
  theme_minimal() 


# ggplotly(Boxplot)
ggplotly(Boxplot) 
```

Column {data-width=350}
-----------------------------------------------------------------------

### Daily temperature difference (celsius) Boxplot by season

```{r ggplotly boxplot , echo = FALSE, warning = FALSE, message = FALSE}

ny_noaa %>% 
  mutate(month = lubridate::month(date),
         year = lubridate::year(date),
         season = case_when(
           month > 2 & month <= 5 ~ 'Spring',
           month > 5 & month <= 8 ~ 'Summer',
           month > 8 & month <= 11 ~ 'Autumn',
           TRUE ~ 'Winter'
         ),
         season = fct_reorder(season, month, max),
         temperature_diff = tmax - tmin) %>% 
  filter(year >= 2000,
         temperature_diff > 0) %>% 
  
  plot_ly(y = ~temperature_diff, color = ~season, type = "box", colors = "viridis") %>% 
    layout(yaxis = list(title = "Daily temperature difference (celsius)"))

```

### Mean Min-temperature(celsius) by Month and color by mean Precipitation (mm)

```{r, echo = FALSE, warning = FALSE, message = FALSE}

ny_noaa %>% 
  mutate(month = lubridate::month(date),
         year = lubridate::year(date)) %>% 
  filter(year >= 2000) %>% 
  group_by(month) %>% 
  summarise(Mean_Prec_mm = mean(prcp)/10,
            mean_tmin = mean(tmin)) %>% 
  plot_ly(x = ~month, y = ~mean_tmin, color = ~Mean_Prec_mm, type = "bar", colors = "Blues") %>% 
    # Change ticks  
    layout(
      # legend = list(title=list(text='Mean Precipitation (mm)')), 
      xaxis = list(
        dtick = 1, 
        tick0 = 1, 
        tickmode = "linear"),
      yaxis = list(title = "Mean Min-temperature (celsius)")
    )
  
    
# rmarkdown::render("dashboard.Rmd", output_format = "flexdashboard::flex_dashboard")

```